package service.DS;

import java.util.*;

import org.apache.axis2.client.Stub;

class serverInfo {
	int type;
	Stub s;
	String addr;
	
	public serverInfo(Stub stub, int t, String addr) {
		this.s = stub;
		this.type = t;
		this.addr = addr;
	}
	
	public Stub getStub() {
		return s;
	}
	
	public int getType() {
		return type;
	}
	
	public String getAddr() {
		return this.addr;
	}
}

public class DSWS {
	public final static int FSLOCAL0 = 0;
	public final static int FSLOCAL1 = 1;
	public final static int FSLAPTOP0 = 10;
	
	private Vector<serverInfo> servers= new Vector<serverInfo>();
	private Iterator<serverInfo> it;
		
	public String getServer(String appName) {
		
		if(servers.isEmpty()) init();
		
		it = servers.iterator();
		String maxServer = null;
		int maxBW = 0;
		
			
			while(it.hasNext()) {
				try{
					int tmp = 0;
					
					serverInfo server = it.next();
					int type = server.getType();
					
					if(type == FSLOCAL0) {
						service.web.local.db1.
                                                        FSWSStub stub = 
                                                        new service.web.local.
                                                        db1.FSWSStub();
						service.web.local.db1.FSWSStub.
                                                        GetBW req = 
                                                        new service.web.local.
                                                        db1.FSWSStub.GetBW();
			        	req.setAppName(appName);
						
			        	service.web.local.db1.FSWSStub.
                                                GetBWResponse res = 
                                                stub.getBW(req);
			        	
			        	tmp = res.get_return();
					}
					else if (type == FSLOCAL1){
						service.web.local.db2.
                                                        FSdb2Stub stub = 
                                                        new service.web.local.
                                                        db2.FSdb2Stub();
						service.web.local.db2.
                                                        FSdb2Stub.GetBW req = 
                                                        new service.web.local.
                                                        db2.FSdb2Stub.GetBW();
			        	req.setAppName(appName);
						
			        	service.web.local.db2.FSdb2Stub.
                                                GetBWResponse res = 
                                                stub.getBW(req);
			        	
			        	tmp = res.get_return();
					}
					else if (type == FSLAPTOP0) {
						service.web.laptop.FSWSStub 
                                                        stub = new service.web.
                                                        laptop.FSWSStub();
						service.web.laptop.FSWSStub.
                                                        GetBW req = 
                                                        new service.web.laptop.
                                                        FSWSStub.GetBW();
			        	req.setAppName(appName);
						
			        	service.web.laptop.FSWSStub.
                                                GetBWResponse res = 
                                                stub.getBW(req);
			        	
			        	tmp = res.get_return();
					}
		            
		            if(tmp > maxBW){
		            	maxBW = tmp;
		            	maxServer = server.getAddr();
		            }
		        } catch(Exception e){
		            e.toString();
		        }
			}
	
		return "Max BW is: " + maxBW + " on service address: "
                        + maxServer;
	}
	
	
	private void init() {
		try {
			servers.add(new serverInfo(new service.web.laptop.
                                FSWSStub(), FSLAPTOP0,
                    "http://http://192.168.0.102:8080/axis2/services/FSWS"));
			servers.add(new serverInfo(new service.web.local.db1.
                                FSWSStub(), FSLOCAL0, 
                    "http://http://localhost:8080/axis2/services/FSWS"));
			servers.add(new serverInfo(new service.web.local.db2.
                                FSdb2Stub(), FSLOCAL1, 
                    "http://http://localhost:8080/axis2/services/FSdb2"));
		}
		catch (Exception e) {
			e.toString();
		}
	}
	
}
